package com.oak.provide;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * com.oak.provide -> OrderController
 *
 * @Author: guoxy
 * @Description:
 * @Date: 2022/5/15
 */
@RestController
@DefaultProperties
public class OrderController {
    @Value("${server.port}")
    private String serverPort;

    /**
     * 可获取eureka及服务的相关参数
     */
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/order")
    private String order() {
        DateTime start = DateUtil.date();
        return StrUtil.format("获取订单【{}】成功，端口：{}，当前时间：{}，接口耗时：{}"
                , UUID.randomUUID(),serverPort, DateUtil.now(),DateUtil.betweenMs(start,DateUtil.date()));
    }

    /**
     * 超时接口测试
     *
     * @return
     */
    @GetMapping("/timeOut")
    private String timeOut() {
        DateTime start = DateUtil.date();
        ThreadUtil.sleep(3000);
        //int num = 1/0;
        return StrUtil.format("获取订单【{}】成功，端口：{}，当前时间：{}，接口耗时：{}"
                , UUID.randomUUID(),serverPort, DateUtil.now(),DateUtil.betweenMs(start,DateUtil.date()));
    }


    @GetMapping("/discoveryInfo")
    private String discoveryInfo() {
        // 服务信息
        return ArrayUtil.join(discoveryClient.getServices().toArray(), StrUtil.COMMA);
    }

}